package com.sysu.simulation;


import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.Map;

import butterknife.BindView;

public class RecordChartActivity extends BaseActivity{

    @BindView(R.id.chart)
    LineChart mChart;
    @BindView(R.id.chart_date)
    TextView mDate;

    //private List<ILineDataSet> dataSets = new ArrayList<>();
    private LineDataSet[] dataSets;
    private LineData data = new LineData();
    private Gson gson = new Gson();
    private static final String[] segName = {"head","trunk","arm","hand","leg","foot"};
    private static final int[] segColor = {Color.BLUE,Color.CYAN,Color.GREEN,Color.MAGENTA,Color.RED,Color.YELLOW};

    public RecordChartActivity() {
        // Required empty public constructor
    }

    @Override
    protected void initWindow() {
        super.initWindow();
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(false);
        }
    }

    public static void show(Context context, Bundle mBundle) {
        Intent mIntent = new Intent(context, RecordChartActivity.class);
        mIntent.putExtras(mBundle);
        context.startActivity(mIntent);
    }

    @Override
    protected int getContentView() {
        return R.layout.fragment_record;
    }

    @Override
    public void onResume() {
        super.onResume();
        Bundle mExtra = getIntent().getExtras();
        if(mExtra != null) {
            if(dataSets == null) {
                dataSets = new LineDataSet[6];
                for(int i=0; i<6; i++) {
                    dataSets[i] = new LineDataSet(new ArrayList<Entry>(), segName[i]);
                    dataSets[i].setColor(segColor[i]);
                    data.addDataSet(dataSets[i]);
                }
                mChart.setData(data);
                mChart.setTouchEnabled(false);
                mChart.getXAxis().setDrawGridLines(false);
                mChart.getAxisLeft().setDrawGridLines(false);
                mChart.getAxisRight().setDrawGridLines(false);
            }
            showChart(mExtra.getString("simresult"));
            mExtra.clear();
        }
    }

    private void showChart(String simresult) {
        if(simresult==null || simresult.isEmpty()) return;
        for(int i=0; i<6; i++) dataSets[i].clear();

        Map<String, JsonElement> paramsMap = gson.fromJson(simresult, new TypeToken<Map<String,JsonElement>>(){}.getType());
        mDate.setText(paramsMap.get("time").getAsString());

        JsonArray records = paramsMap.get("records").getAsJsonArray();
        for(int i=0; i<records.size(); i++) {
            JsonObject record = records.get(i).getAsJsonObject();
            float time = record.getAsJsonPrimitive("time").getAsFloat();
            JsonArray temps = record.getAsJsonArray("temperature");
            for(int j=0; j<6; j++) {
                dataSets[j].addEntry(new Entry(time,temps.get(j).getAsFloat()));
            }
        }

        data.notifyDataChanged();
        mChart.notifyDataSetChanged();
        mChart.invalidate();
    }
}